package xyz.yuzegod.megawalls.util;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.util.io.BukkitObjectInputStream;
import org.bukkit.util.io.BukkitObjectOutputStream;
import xyz.yuzegod.megawalls.MegaWalls;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

public class ItemUtils
{
    public static byte[] write(final ItemStack itemStack) {
        try {
            final ByteArrayOutputStream out = new ByteArrayOutputStream();
            final BukkitObjectOutputStream bukkitOut = new BukkitObjectOutputStream((OutputStream)out);
            bukkitOut.writeObject((Object)itemStack);
            bukkitOut.close();
            return out.toByteArray();
        }
        catch (Exception e) {
            e.printStackTrace();
            return new byte[0];
        }
    }
    
    public static ItemStack read(final byte[] bytes) {
        try {
            final BukkitObjectInputStream bukkitIn = new BukkitObjectInputStream((InputStream)new ByteArrayInputStream(bytes));
            return (ItemStack)bukkitIn.readObject();
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    public static boolean isSword(final ItemStack is) {
        switch (is.getType()) {
            case WOOD_SWORD:
            case IRON_SWORD:
            case GOLD_SWORD:
            case DIAMOND_SWORD: {
                return true;
            }
            default: {
                return false;
            }
        }
    }
    public static boolean containsBow(List<ItemStack> is){
        for (ItemStack it:is){
            if (it.getType().equals(Material.BOW)){
                return true;
            }
        }
        return false;
    }
    
    public static boolean isSpade(final ItemStack is) {
        switch (is.getType()) {
            case WOOD_SPADE:
            case IRON_SPADE:
            case GOLD_SPADE:
            case DIAMOND_SPADE: {
                return true;
            }
            default: {
                return false;
            }
        }
    }
    
    public static boolean isHelmet(final ItemStack is) {
        switch (is.getType()) {
            case LEATHER_HELMET:
            case CHAINMAIL_HELMET:
            case IRON_HELMET:
            case GOLD_HELMET:
            case DIAMOND_HELMET: {
                return true;
            }
            default: {
                return false;
            }
        }
    }
    
    public static boolean isChestplate(final ItemStack is) {
        switch (is.getType()) {
            case LEATHER_CHESTPLATE:
            case CHAINMAIL_CHESTPLATE:
            case IRON_CHESTPLATE:
            case GOLD_CHESTPLATE:
            case DIAMOND_CHESTPLATE: {
                return true;
            }
            default: {
                return false;
            }
        }
    }
    
    public static boolean isLeggings(final ItemStack is) {
        switch (is.getType()) {
            case LEATHER_LEGGINGS:
            case CHAINMAIL_LEGGINGS:
            case IRON_LEGGINGS:
            case GOLD_LEGGINGS:
            case DIAMOND_LEGGINGS: {
                return true;
            }
            default: {
                return false;
            }
        }
    }
    
    public static boolean isBoots(final ItemStack is) {
        switch (is.getType()) {
            case LEATHER_BOOTS:
            case CHAINMAIL_BOOTS:
            case IRON_BOOTS:
            case GOLD_BOOTS:
            case DIAMOND_BOOTS: {
                return true;
            }
            default: {
                return false;
            }
        }
    }
    public static boolean isFullInventory(Inventory inventory) {
        boolean result = true;
        for (ItemStack is : inventory) {
            if (is == null) {
                return false;
            }
        }
        return result;
    }
    public static boolean isResource(Block b){
        return matchMaterial(b,Material.DIRT,Material.GRASS,Material.SAND,Material.GRAVEL,Material.MYCEL,Material.STONE , Material.COBBLESTONE ,Material.COAL_ORE , Material.IRON_ORE , Material.GOLD_ORE , Material.LOG ,Material.LOG_2 );
    }
    public static boolean isDirtResource(Block b){
        return matchMaterial(b,Material.DIRT,Material.GRASS,Material.SAND,Material.GRAVEL,Material.MYCEL);
    }
    public static boolean isOreResource(Block b){
        return matchMaterial(b,Material.STONE , Material.COBBLESTONE ,Material.COAL_ORE , Material.IRON_ORE , Material.GOLD_ORE);
    }
    public static boolean exceptDirt(Block b){
        return matchMaterial(b,Material.STONE , Material.COBBLESTONE ,Material.COAL_ORE , Material.IRON_ORE , Material.GOLD_ORE,Material.LOG,Material.LOG_2);
    }
    public static boolean matchMaterial(Block b, Material...l ){
        Material type=b.getType();
        if (!MegaWalls.getInstance().getGame().isWallsFall()){
            for (Material m:l){
                if (type.equals(m)){
                    return true;
                }
            }
            for (Material m:l){
                if (b.hasMetadata("type")){
                    if (((FixedMetadataValue) b.getMetadata("type").get(0)).value().equals(m)){
                        return true;
                    }
                }
            }
        }else {
            for (Material m:l){
                if (type.equals(m)){
                    return true;
                }
            }
        }
        return false;
    }
}
